//
//  SecondViewController.m
//  UI_Homework_11
//
//  Created by 黄威 on 16/5/17.
//  Copyright © 2016年 黄威. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()
@property (nonatomic,retain)UITextField *tfname;
@property (nonatomic,retain)UITextField *tfage;
@property (nonatomic,retain)UITextField *tfphonenumber;
@property (nonatomic,retain)NSMutableDictionary *DIC;
@property (nonatomic,retain)NSMutableArray *MARR;
@property (nonatomic,retain)NSMutableDictionary *dic;
@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTextField];
    [self configNavigationItem];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)configNavigationItem{

self.navigationItem.title = @"创建🐰修改";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)];
    self.view.backgroundColor = [UIColor purpleColor];
}

- (void)createTextField{

    self.tfname = [[UITextField alloc]initWithFrame:CGRectMake(80, 100, 200, 40)];

   self.tfname.placeholder = @"姓名";
    self.tfname.text = self.Name;
    
    
    self.tfage = [[UITextField alloc]initWithFrame:CGRectMake(80, 200, 200, 40)];
    
    self.tfage.placeholder = @"年龄";
    self.tfage.text = self.Age;
    
    
    self.tfphonenumber = [[UITextField alloc]initWithFrame:CGRectMake(80, 300, 200, 40)];
    
    self.tfphonenumber.placeholder = @"电话号码";
    self.tfphonenumber.text = self.Phonenumber;

    
    self.tfphonenumber.backgroundColor = [UIColor purpleColor];
    self.tfage.backgroundColor = [UIColor purpleColor];
    self.tfname.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:_tfname];
    [self.view addSubview:_tfage];
    [self.view addSubview:_tfphonenumber];
}


- (void)save{
    NSString *name = self.tfname.text;
    NSString *age = self.tfage.text;
    NSString *phonenumber =self.tfphonenumber.text;


    self.dic = @{@"name":name,
                 @"age":age,
                 @"phonenumber":phonenumber}.mutableCopy;
    
    self.MARR = [[NSMutableArray alloc]init];
    [self.MARR addObject:self.dic];
    NSString *str = [name substringToIndex:1].uppercaseString;
    self.DIC = [@{str:self.MARR} mutableCopy];
    
    
    NSString *pathOfDocument = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSLog(@"%@", pathOfDocument);
    NSString *pathOfFile = [pathOfDocument stringByAppendingPathComponent:@"Contact.plist"];
    NSLog(@"%@", pathOfFile);
    
    [self.DIC writeToFile:pathOfFile atomically:YES];
    
    [self.navigationController popToRootViewControllerAnimated:YES];


}










/*
 
 
 
 
 
 
 
 
 
 
 
 
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
